from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.views import generic
from echart.models import Chart, DataItem, ItemClass, FwNewsPowerClass, AiImgModel
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.clickjacking import xframe_options_exempt
from django.contrib.auth.decorators import permission_required
from django.contrib.auth import authenticate, login, logout
from vis_sys.settings import ELASTIC_HOST,FW_NEW_INDEX,FW_NEW_TYPE, NANHAI,EMOTION_LINE,FOSHAN,BASE_DIR, ZONES,\
    HOTWORD_TYPE,HOTWORD_INDEX, MEDIA_ROOT

from echart.mongo_db_help import fing_lastest_range, get_last_time, find_lastest_zone_range
from echart.baidu import baidu_hot_range, get_key, baidu_province, ZONE_CODE,THEME_CODE
from echart.forms import AuthenticationForm
import echart.fanwen_api as fanwen
import echart.ts_tools as tstools
# from echart.nlp_tool import check_one, check_fsp_ids
from echart.ai_tools import class_image, trans_api, rubbish, handle_uploaded_file

from collections import OrderedDict
import json
from datetime import datetime,timedelta
import time
from random import sample
import pandas as pd
import uuid
import os
from echart.elastic_tool import News, ArticletypeStatic, topN_media_static,get_article, EmotionLog, \
    get_zone_news_static,MessageNews

from echart.baidu_api import find_keyword, comment_tag
from PIL import Image

def allow_all(response):
    """
    解决跨域的问题
    :param response:
    :return:
    """
    response["Access-Control-Allow-Origin"] = "*"
    response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
    response["Access-Control-Max-Age"] = "1000"
    response["Access-Control-Allow-Headers"] = "*"
    return response


def news_permissions(username, path):
    has_permission = False
    permission_list = FwNewsPowerClass.objects.filter(user__username=username, name=path)
    if permission_list:
        has_permission = True
    return has_permission

def LoginView(request):
    """
    Log in view
    """
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        content = {}
        if form.is_valid():
            user = authenticate(username=request.POST['username'], password=request.POST['password'])
            if user is not None:
                login(request, user)
                return HttpResponseRedirect('/')
            else:
                content['info'] = u"帐号或密码错误"
                content['form'] = AuthenticationForm()
                return render(request, 'login.html', content)
    else:
        form = AuthenticationForm()

    return render(request, 'login.html', {'form': form})

def LogoutView(request):
    logout(request)
    return redirect('/')


class ChartIndexView(generic.ListView):
    model = Chart
    template_name = "chart_index.html"
    paginate_by = 10   # 一个页面显示的条目
    context_object_name = "chart_list"


def chart_page(request, p_id):
    return render(request, 'chart.html', {"page": str(p_id)})

@csrf_exempt
def get_chart(request, p_id):
    chart = get_object_or_404(Chart, pk=p_id)

    datas = DataItem.objects.filter(chart=chart).order_by('item_date')
    list_name = []
    content = {
        'datas':OrderedDict(),
        'color_list':{},
        'title':chart.title,
        'paramets': json.loads(chart.parameters)
    }
    for data in datas:
        date = data.item_date.strftime('%Y/%m/%d')
        if not data.name in list_name:
            list_name.append(data.name)

        if not date in content['datas']:
            content['datas'][date] = []

        content['datas'][date].append({'name': data.name, 'value': data.value})

    #符合格式
    """
    [{'date': '2011-09-05', 'data': [{'name': '江苏', 'value': 120}]}, 
    {'date': '2011-10-06', 'data': [
        {'name': '广东xxxxxxxxx8978978974565131567987', 'value': 300},
        {'name': '江苏', 'value': 140}
    ]}]
    """
    tmp_datas = []
    for key, value in content['datas'].items():
        tmp_datas.append({
            'date': key,
            'data': value
        })

    content['datas'] = tmp_datas

    colors = ItemClass.objects.filter(name__in=list_name)

    for color in colors:
        content['color_list'][color.name] =color.color

    #return render(request, 'chart.html', content)
    response =  HttpResponse(json.dumps(content), content_type="application/json")
    return allow_all(response)


def load_data(request):
    f = request.FILES.get('file')
    chart = None
    if request.method == 'POST' and f:
        try:
            if f.name.split('.')[1] == 'csv':
                # 如果是csv
                data_from_file = pd.read_csv(f)
            else:
                # 如果不是，默认是excel
                data_from_file = pd.read_excel(f)

            data_from_file['date'] = pd.to_datetime(data_from_file['date'], format = '%Y/%m/%d', errors = 'ignore')
            input_data_list = []
            chart =  Chart(
                title=f.name.split('.')[0],
                parameters='{"maxValue":600,"ShowNumber": 7,"feq":500}'
            )
            chart.save()
            def add_to_list(row):
                input_data_list.append(DataItem(
                    name=row['name'],
                    value=row['value'],
                    item_date=row['date'].strftime('%Y-%m-%d %H:%M:%S'),
                    chart=chart,
                ))

            data_from_file.apply(add_to_list, axis=1)

            DataItem.objects.bulk_create(input_data_list)

        except Exception as e:
            return JsonResponse({
                "message":'读取文档出错，请确保确认文档格式是否正确，错误信息：{}'.format(e),
                "status":'Error'
            })

    content={'status': 'OK'}
    if chart:
        content['chart']= 'http://{0}/chart/{1}'.format(request.META.get('HTTP_HOST'), chart.id)

    return JsonResponse(content)


def get_baidu_range(request):
    type = request.GET.get('type')
    # 判断信息是否过期, 最新１小时内容
    try:
        last_time = get_last_time()
    except:
        return allow_all(JsonResponse({'is_error': True, 'msg': '数据库出错'}))

    now = datetime.strptime(datetime.today().strftime('%Y/%m/%d %H'), '%Y/%m/%d %H')
    if last_time < now:
        # 如果不是最新，马上到百度上获取
        baidu_hot_range()

    res = fing_lastest_range(type=type)
    content = {'is_error': False, 'datas': res}
    return allow_all(JsonResponse(content))

def get_baidu_zone_range(request):

    zone_name = request.GET.get('zone')
    theme_name = request.GET.get('theme')
    content = {}

    if zone_name and theme_name:
        zone_code = get_key(ZONE_CODE,zone_name)
        if len(zone_code) == 0:
            return allow_all(JsonResponse({'is_error': True, 'msg': '暂时没有包含这地区'}))
        zone_code = zone_code[0]


        theme_code = get_key(THEME_CODE, theme_name, 'name')
        if len(theme_code) == 0:
            return allow_all(JsonResponse({'is_error': True, 'msg': '没有这个主题'}))

        theme_code = theme_code[0]

        # 判断信息是否过期, 最新１小时内容
        last_time = get_last_time({'zone': zone_name, 'name': theme_name})
        now = datetime.strptime(datetime.today().strftime('%Y/%m/%d %H'), '%Y/%m/%d %H')
        if last_time < now:
            # 如果不是最新，马上到百度上获取
            result = baidu_province(zone_code,theme_code)
            if result['is_error']:
                return allow_all(JsonResponse(result))

        res = find_lastest_zone_range({'zone_id':zone_code, 'key':theme_code})
        content.update({'is_error': False, 'datas': res})

    else:
        content.update({'is_error': True, 'msg': '缺少参数'})

    return allow_all(JsonResponse(content))


def baidu_page(request):
    return render(request, 'baidu_range.html')


def baidu_zone_page(request):
    return render(request, 'baidu_zone.html')


def fanwen_media_focus(request):
    num = request.GET.get('num',6)
    start = request.GET.get('start',0)
    orderby = request.GET.get('orderby', 'samecount+desc')

    return JsonResponse(fanwen.get_data(num=num, start=start, orderby=orderby))


def fanwen_toutiao(request):
    num = request.GET.get('num',6)
    start = request.GET.get('start',0)
    type_id = request.GET.get('id','14162')
    return JsonResponse(fanwen.get_toutiao_data(type_id=type_id,num=num, start=start))


def fanwen_fs_news(request):

    num = request.GET.get('num', 5)
    start = request.GET.get('start',0)
    page_id = request.GET.get('page_id', '53215') # 默认是佛山日报
    try:
        num = int(num)
    except:
        num = 5

    endDate = request.GET.get('endDate')
    today = datetime.today()
    if not endDate:
        endDate = today.strftime("%Y-%m-%d")

    startDate = request.GET.get('startDate')
    if not startDate:
        startDate = today - timedelta(days=1)
        startDate = startDate.strftime("%Y-%m-%d")

    if endDate < startDate:
        content = {'Succeed': False, 'Msg': '开始日期大于结束日期'}
    else:
        res = fanwen.get_newsroute(page_id, startDate, endDate , start=start)
        content = {'rows':[], 'Succeed': True}
        for item in res['rows'][:num]:
            content['rows'].append({
                'titiel': item['title'],
                'count': item['same_count'],
                'class': item['class1'],
                'papername': item['papername'],
                'editor': item['editor'],
                'createtime': item['createtime'],

            })

    return allow_all(JsonResponse(content))


def fanwen_fs_diffusion(request):
    articleid = request.GET.get('articleid')
    if articleid:
        today = datetime.today().strftime('%Y-%m-%d')
        updatetime = request.GET.get('updatetime', today)
        return JsonResponse(fanwen.get_diffusion_info(articleid, updatetime))
    else:
        num = request.GET.get('num', 5)
        start = request.GET.get('start', 0)
        res = fanwen.get_foshanNews(num=num, start=start)

        try:
            num = int(num)
        except:
            num = 5

        items = []
        for item in res['obj']['rows'][:num]:
            items.append({
                'title': item['title'],
                'increment': abs(item['increment']),
                'class': item['class1'],
                'updatetime': item['updatetime'],
                'degree': item['degree'],
                'sameid1': item['sameid1'],
            })
        return render(request, 'fsnews.html', {"news_list": items})


def fanwen_use_hot(request):
    num = request.GET.get('num',6)
    start = request.GET.get('start',0)
    today = datetime.today().strftime('%Y-%m-%d')
    from_date = request.GET.get('startDate',today)
    to_date = request.GET.get('endDate', today)
    orderby = request.GET.get('orderby', 'degree+desc')
    return JsonResponse(fanwen.get_user_hot(from_date, to_date,num=num, start=start, orderby=orderby))


def fanwen_statistic(request):
    return allow_all(JsonResponse(fanwen.get_statistics()))

@xframe_options_exempt
def fanwen_newsBM(request):
    ids = request.GET.get('ids')
    if ids:
        return allow_all(JsonResponse(fanwen.get_newsBM(ids)))
    else:
        return allow_all(JsonResponse(fanwen.get_newsBM()))


def get_boxoffice(request):
    method = request.GET.get('method', 'now')
    content = {}
    try:
        if method == 'now':
            content['now_list'] = tstools.get_realtime_boxoffice()
        elif method == 'month':
            date = request.GET.get('date', None)
            content['month_list'] = tstools.get_month_boxoffice(date)
        else:
            date = request.GET.get('date', None)
            content['day_list'] = tstools.get_day_boxoffice(date)
    except Exception as e:
        content['info'] = e

    return render(request, 'boxoffice.html', content)


def get_contry_info(request):
    info_id = request.GET.get('info_id', 'ppi')

    if info_id == 'ppi':
        return JsonResponse(tstools.get_ppi(),safe=False)

    if info_id == 'rrr':
        return JsonResponse(tstools.get_rrr(), safe=False)

    return JsonResponse({'info': '没有东西'})



def fanwen_emotion(request):
    channelid = request.GET.get('channelid')
    if channelid not in ['86331','86325','86324','86329','86328','86326','86327','86323','86332','86330']:
        res = JsonResponse({'is_error': True, 'msg': '频道id出错'})
        return allow_all(res)

    emotion = request.GET.get('emotion', '51')

    if emotion not in ['1', '51']:
        res = JsonResponse({'is_error':True, 'msg':'情感id出错'})
        return allow_all(res)

    # 一共7天时间
    end_date = datetime.today()
    start_date = end_date - timedelta(days=6)


    res = JsonResponse(fanwen.get_emotion_info(
        channel_id=channelid,emotion_id=emotion,start_date=start_date,end_date=end_date))
    return allow_all(res)


def emotion_chart(request):

    emotion = request.GET.get('emotion', '51')
    zone_id = request.GET.get('zone_id')

    if emotion == '51':
        channels = [
            {'zone': '顺德区', 'id': '86327', 'emotion': '51'},
            {'zone': '南海区', 'id': '86325', 'emotion': '51'},
            {'zone': '禅城区', 'id': '86324', 'emotion': '51'},
            {'zone': '三水区', 'id': '86331', 'emotion': '51'},
            {'zone': '高明区', 'id': '86329', 'emotion': '51'},

        ]
    else:
        channels = [
            {'zone': '顺德区', 'id': '86328', 'emotion': '1'},
            {'zone': '南海区', 'id': '86326', 'emotion': '1'},
            {'zone': '禅城区', 'id': '86323', 'emotion': '1'},
            {'zone': '三水区', 'id': '86332', 'emotion': '1'},
            {'zone': '高明区', 'id': '86330', 'emotion': '1'},
        ]

    end_date = datetime.today()
    start_date = end_date - timedelta(days=7)
    content = {'x_axis':[]}
    content['datas'] = []
    content['legend'] = []
    tmp_data = {}
    for item in channels:
        content['legend'].append(item['zone'])
        tmp_data[item['zone']] = []

    e_log = EmotionLog()
    try:
        res = e_log.emotion_total(begin_date=start_date.strftime('%Y-%m-%d'), end_date=end_date.strftime('%Y-%m-%d'),
                                  emotion=emotion, zone_id=zone_id)
        res['is_success'] = True
    except Exception as e:
        print(e)
        res = {'is_success':False}

    if res['is_success']:
        for date, value in res['data'].items():
            content['x_axis'].append(date)
            for v in value:
                tmp_data[v['zone']].append(v['total'])

        for key, value in tmp_data.items():
            series = {'name':key, 'values':value}
            content['datas'].append(series)
    else:
        for item in channels:
            content['datas'].append({'name':item['zone'], 'values':['1']*7})

    return allow_all(JsonResponse(content))


def foshan_hot_word(request):
    count = request.GET.get('num', "10")
    exclude = request.GET.get('exclude')

    res = fanwen.hot_word(count=count, exclude=exclude)

    return allow_all(JsonResponse(res))


def fsnews_api(request):
    count = request.GET.get('num', "10")
    start = request.GET.get('start', '0')

    res = fanwen.foshan_news_api(count=count, start=start)
    return allow_all(JsonResponse(res))


def get_leaderids(request):
    return_format = request.GET.get('format', 'html')
    res = fanwen.get_person_id()
    if return_format == 'html':
        return render(request, 'leader.html', {'persons':res['focusPerson']})
    else:
        return JsonResponse({
            'is_error': False,
            'persons': res['focusPerson']
        })


def get_leader_news_keyword(request):
    """
    通过fwnewspowerclass的配置，去限制用户获取链接权限
    username,查看是否有这个链接的权限
    :param request: 
    :return: 
    """
    person_id = request.GET.get('personid')
    user_name = request.GET.get("user")
    has_premiss = False

    if user_name:
        path = request.META.get('PATH_INFO')
        has_premiss = news_permissions(user_name, path[1:])

    if person_id and has_premiss:
        count = request.GET.get('num', '10')
        end = request.GET.get('endDate')
        start = request.GET.get('startDate')
        if not end:
            end = datetime.today()

            if not start:
                start = end - timedelta(days=30)
                start = start.strftime('%Y-%m-%d')

            end = end.strftime('%Y-%m-%d')
        keywords = fanwen.person_news_keyword(person_id, start, end, count=count)

        return JsonResponse({
            'is_error': False,
            'data':keywords
        })

    return JsonResponse({'is_error': True, 'msg': '缺少参数或权限'})


def get_leader_news(request):
    person_id = request.GET.get('personid')
    name = request.GET.get('name')
    count = request.GET.get('num', '10')
    curPage = request.GET.get('page', '1')
    host = request.get_host()

    if person_id and name:
        news = fanwen.person_news(person_id, curPage=curPage,count=count)
        for item in news['data']:
            item['url'] = "http://{host}/leader_detail?actical_id={actical_id}&name={name}".format(
                actical_id=item['id'],
                name=name,
                host=host
            )

        return JsonResponse({
            'is_error': False,
            'data': news
        })

    return JsonResponse({'is_error': True, 'msg': '缺少参数'})


@permission_required('echart.view_fwnewspowerclass',raise_exception=True)
#通过后台用户权限来实现
def leader_static(request):
    persons = request.GET.get('persons')
    if persons:
        result = []
        persons = persons.split(',')
        leaders = fanwen.get_person_id()['focusPerson']

        startDate = request.GET.get('startDate')
        if startDate:
            today = datetime.today()
            endDate = request.GET.get('endDate', today.strftime("%Y-%m-%d"))
            try:
                startDate = time.strptime("{} 00:00:00".format(startDate), "%Y-%m-%d %H:%M:%S")
                endDate = time.strptime("{} 23:59:59".format(endDate), "%Y-%m-%d %H:%M:%S")
                if endDate < startDate:
                    return JsonResponse({'is_error': True, 'msg': '开始时间大于结束时间'})
            except:
                return JsonResponse({'is_error': True, 'msg': '输入参数错误'})

            for person in persons:

                for leader in leaders:
                    if leader['name'] == person:
                        page = 1
                        total_row = 0
                        not_break = True
                        while not_break:
                            news = fanwen.person_news(leader['personId'], curPage=page, count=100)
                            if len(news['data']) == 0:
                                break

                            for data in news['data']:
                                begin_index = data['updatetime'].find('(')
                                end_index = data['updatetime'].find('+')
                                news_date = time.localtime(
                                    float(data['updatetime'][begin_index + 1:end_index]) / 1000)
                                if startDate <= news_date:
                                    if news_date <= endDate:
                                        total_row += 1
                                else:
                                    not_break = False
                                    break
                            page += 1

                        result.append({
                            'name': leader['name'],
                            'id': leader['personId'],
                            'totalRows': total_row
                        })

                        break
        else:
            for person in persons:
                for leader in leaders:
                    if leader['name'] == person:
                        news = fanwen.person_news(leader['personId'], curPage=1, count=1)
                        result.append({
                            'name': leader['name'],
                            'id': leader['personId'],
                            'totalRows': news['totalRows']
                        })
                        break
        return JsonResponse({'is_error': False, 'reslut': result})

    return JsonResponse({'is_error': True, 'msg': '缺少参数'})



def get_detail_news(request):
    actical_id = request.GET.get('actical_id')
    name = request.GET.get('name')

    if actical_id and name:
        news = fanwen.person_actical(actical_id, name)

        return JsonResponse({
            'is_error': False,
            'data': news
        })

    return JsonResponse({'is_error': True, 'msg': '缺少参数'})


def search_page(request):
    return render(request,'advanced_search_page.html')

def advanced_search_actical(request):
    allKey = request.GET.get('allKey', '')
    anyKey = request.GET.get('anyKey', '')

    if allKey !='' or anyKey !="":
        exceptKey = request.GET.get('exceptKey', "")
        start = request.GET.get('start', '0')
        limit = request.GET.get('limit', '30')
        startDate = request.GET.get('startDate')
        endDate = request.GET.get('endDate')

        if not endDate:
            endDate = datetime.today()

            if not startDate:
                startDate = endDate - timedelta(days=0)
                startDate = startDate.strftime('%Y-%m-%d')

            endDate = endDate.strftime('%Y-%m-%d')

        emotion = request.GET.get('emotion', '-1')
        selType = request.GET.get('selType', '')
        searchRange = request.GET.get('searchRange', '1')

        res = fanwen.advancedsearch(allKey, anyKey, startDate, endDate, exceptKey=exceptKey, start=start,
                                    limit=limit, emotion=emotion, selType=selType, searchRange=searchRange)

        return JsonResponse({
            'is_error': True,
            'msg': '成功',
            'news': res['obj']['rows'],
            'total': res['obj']['total'],
        })

    return JsonResponse({'is_error': True, 'msg': '缺少参数'})


def foshan_emotions_page(request):
    qu_ids = {'禅城正面新闻':'86324','禅城负面新闻':'86323','南海正面新闻':'86325','南海负面新闻':'86326','顺德正面新闻':'86327',
    '顺德负面新闻':'86328','高明正面新闻':'86329','高明负面新闻':'86330','三水正面新闻':'86331','三水负面新闻':'86332',
              '微信':'14162', '网络媒体': "12839", "党媒":"15628", "佛山新闻":"6934"}
    res = []
    for name, id in qu_ids.items():
        res.append({'name':name, 'id':id})

    return render(request, "foshan_emotion_page.html", {'qu_list':res})

def foshan_emotions_news(request):
    """
    获取佛山各区的正负面新闻信息
    和微信,网络,媒体,党媒,佛山新闻栏目用同一个接口，栏目id号不同
    """
    qu_id = request.GET.get('id')

    if qu_id:
        start = request.GET.get('start', '0')
        limit = request.GET.get('limit', '20')
        selType = request.GET.get('selType', '')
        classify = request.GET.get('classify', '')
        keyword = request.GET.get('keyword', '')
        startDate = request.GET.get('startDate')
        endDate = request.GET.get('endDate')

        if not endDate:
            endDate = datetime.today()

            if not startDate:
                startDate = endDate - timedelta(days=0)
                startDate = startDate.strftime('%Y-%m-%d')

            endDate = endDate.strftime('%Y-%m-%d')

            res = fanwen.get_foshan_qu_news(qu_id, startDate, endDate, start=start, limit=limit,
                                            selType=selType, keyWords=keyword, classify=classify)

            return JsonResponse({
                'is_error': True,
                'msg': '成功',
                'news': res['obj']['rows'],
                'total': res['obj']['total'],
            })

    return JsonResponse({'is_error': True, 'msg': '缺少参数'})


def fsrb_distribution(request):
    today = datetime.today()
    page_id = request.GET.get('page_id', '53215') # 默认是佛山日报
    endDate = request.GET.get('endDate', today.strftime("%Y-%m-%d"))
    startDate = request.GET.get('startDate')
    if not startDate:
        startDate = endDate

    return JsonResponse(fanwen.source_static(page_id=page_id, startDate=startDate,endDate=endDate))


def get_city_by_code(request):
    path = BASE_DIR + '/echart/city_code.pkl'
    res = fanwen.load_pkl(path)
    code = request.GET.get('code')
    city = None
    provence = None
    if code:
        city = res['citys'].get(code)
        provence = res['provinces'].get(code)
    return JsonResponse({'is_error': False, 'city':city, 'provence':provence,'data': res})
# ===================Elasticsearch API ===================================

def local_news(request):
    """
    获取文章
    参数：
    开始时间
    结束时间
    """
    # TODO:增加参数:类型，情感，排序，文章来源

    local_news = News(host=ELASTIC_HOST, index_name=FW_NEW_INDEX, type_name=FW_NEW_TYPE)
    today = datetime.today()

    s = local_news.search()
    s = s.source(fields={
        'excludes':["old_versions", "news_desc", "news_image"]
    })
    category_id = request.GET.get('category_id')

    if category_id:
        s = s.filter("term", category_id=category_id)

    endDate = request.GET.get('endDate', today.strftime("%Y-%m-%d"))
    startDate = request.GET.get('startDate')
    if not startDate:
        startDate = endDate

    s = s.query('range', update_time={
        "gte": "{} 00:00:00.000".format(startDate),
        "lte": "{} 23:59:59.999".format(endDate)
    })
    s = s.sort('-update_time')
    size = int(request.GET.get('size', '30'))
    from_id = int(request.GET.get('from', '0'))
    response = s[from_id:from_id+size].execute()

    res = JsonResponse({
        'is_success': True,
        'result':{
            'total': response.hits.total,
            'took': response.took,
            'rows': [item['_source'] for item in response.hits.hits]
        }
    })

    return allow_all(res)


def article_source(request):
    """
    文章来源统计
    """
    today = datetime.today()
    article = ArticletypeStatic()
    endDate = request.GET.get('endDate', today.strftime("%Y-%m-%d"))
    startDate = request.GET.get('startDate')
    if not startDate:
        startDate = endDate

    category_id = request.GET.get('category_id', NANHAI)

    res = article.article_static(category_id,startDate,endDate)
    return JsonResponse({'is_success': True, 'result': res})


def article_static_days(request):
    """
    按时间返回文章统计
    """
    local_news = News(host=ELASTIC_HOST, index_name=FW_NEW_INDEX, type_name=FW_NEW_TYPE)
    today = datetime.today()
    date_format = "%Y-%m-%d"
    s = local_news.search()

    category_id = request.GET.get('category_id')
    if category_id:
        s = s.filter("term", category_id=category_id)

    endDate = request.GET.get('endDate', today.strftime(date_format))
    startDate = request.GET.get('startDate')

    if not startDate:
        startDate = endDate

    s = s.query('range', update_time={
        "gte": "{} 00:00:00.000".format(startDate),
        "lte": "{} 23:59:59.999".format(endDate)
    })

    s.aggs.bucket("group_by_time", "date_histogram", field="update_time", interval="day")
    response = s[0].execute()
    result = {}
    for term in response.aggregations.group_by_time.buckets:
        result[term.key_as_string] = term.doc_count
    return JsonResponse({'is_success': True, 'result': result})


def article_static_days_and_source(request):
    """
    按时间分类，文章来源统计    
    """
    today = datetime.today()
    article = ArticletypeStatic()
    endDate = request.GET.get('endDate', today.strftime("%Y-%m-%d"))
    startDate = request.GET.get('startDate')
    if not startDate:
        startDate = endDate

    category_id = request.GET.get('category_id', NANHAI)

    res = article.article_static_per_day(category_id, startDate, endDate)

    return JsonResponse({'is_success': True, 'result': res})

def get_emotion_static(request):
    """
    文章情感分类
    
    """
    # today = (datetime.today()-timedelta(days=1)).strftime("%Y-%m-%d")
    local_news = News(host=ELASTIC_HOST, index_name=FW_NEW_INDEX, type_name=FW_NEW_TYPE)
    today = datetime.today()

    s = local_news.search()
    s = s.source(fields={
        'excludes': ["old_versions", "news_desc", "news_image"]
    })
    category_id = request.GET.get('category_id')

    if category_id:
        s = s.filter("term", category_id=category_id)

    endDate = request.GET.get('endDate', today.strftime("%Y-%m-%d"))
    startDate = request.GET.get('startDate')
    if not startDate:
        startDate = endDate

    s = s.query('range', update_time={
        "gte": "{} 00:00:00.000".format(startDate),
        "lte": "{} 23:59:59.999".format(endDate)
    })

    s_bad = s.query('range', VideoSize={
        "lte": EMOTION_LINE
    })

    resp_bad = s_bad[0].execute()
    resp_all = s[0].execute()
    result = {
        'bad' : resp_bad.hits.total,
        'all' : resp_all.hits.total
    }

    return JsonResponse({'is_success': True, 'result': result})


def get_media_static(request):
    today = datetime.today()
    endDate = request.GET.get('endDate', today.strftime("%Y-%m-%d"))
    startDate = request.GET.get('startDate')
    if not startDate:
        startDate = endDate

    category_id = request.GET.get('category_id', NANHAI)
    top_n = request.GET.get('topn', '10')
    try:
        top_n =  int(top_n)
    except:
        top_n = 10
    result = topN_media_static(int(top_n), startDate,endDate, category_id)
    return JsonResponse({'is_success': True, 'result': result})


def get_similar_article(request):
    article_id = request.GET.get('article_id')
    if article_id:
        today = datetime.today()
        date_format = "%Y-%m-%d"
        category_id = request.GET.get('category_id', FOSHAN)
        endDate = request.GET.get('endDate', today.strftime(date_format))
        startDate = request.GET.get('startDate')
        if not startDate:
            startDate = endDate
        emotion = request.GET.get('emotion', 'all')
        source = request.GET.get('source', 'all')

        from_item = request.GET.get('from_item', '0')
        to_item = request.GET.get('to_item', '50')
        try:
            from_item = int(from_item)
            to_item = int(to_item)
            if to_item <= from_item:
                return JsonResponse({'is_success': False, 'result': [], 'msg': 'from_item必须比to_item小'})
        except:
            return JsonResponse({'is_success': False, 'result': [], 'msg': 'from_item和to_item需要填写数字'})

        res = get_article(
            article_id,
            category_id=category_id,
            from_item=from_item,
            to_item=to_item,
            beginDate=startDate,
            endDate=endDate,
            emotion=emotion,
            source=source
        )
        if res:
            return JsonResponse({'is_success': True, 'result': res})
        else:
            return JsonResponse({'is_success': True, 'result': []})

    return JsonResponse({'is_success': False, 'result': [], 'msg':'缺少参数'})


def get_emotion_by_zone(request):
    """
    数据是来自凡闻正负面,数据记录在本地
    :param request: 
    :return: 
    """
    today = datetime.today()
    endDate = request.GET.get('endDate', today.strftime("%Y-%m-%d"))
    startDate = request.GET.get('startDate')
    if not startDate:
        startDate = endDate
    emotion = request.GET.get('emotion', '1')
    zone_id = request.GET.get('zone_id')
    e_log = EmotionLog()
    try:
        result = e_log.emotion_total(begin_date=startDate, end_date=endDate, emotion=emotion, zone_id=zone_id)
        res = JsonResponse({'is_success': True, 'result': result})
    except Exception as e:
        print(e)
        res = JsonResponse({'is_success': False, 'result': {}})

    return allow_all(res)


def get_zone_static(request):
    zone_id = request.GET.get('zone_id')
    if zone_id:
        today = datetime.today()
        date_format = "%Y-%m-%d"

        endDate = request.GET.get('endDate', today.strftime(date_format))
        startDate = request.GET.get('startDate')

        if not startDate:
            startDate = endDate

        result = get_zone_news_static(zone_id, ZONES[zone_id], startDate, endDate)

        return JsonResponse({'is_success': True, 'result': result})
    else:
        return JsonResponse({'is_success': False, 'msg': "缺少zone_id"})

def get_fw_event_list(request):
    format = request.GET.get('format', 'html')
    events = fanwen.get_eventlist()
    if format == 'json':
        return JsonResponse(events)
    return render(request,'fw_event.html', {'events': events['nav']})


def event_detail(requets):
    event_id = requets.GET.get('event_id')

    if event_id:
        province = requets.GET.get('province', '北京')
        default_day = datetime.today().strftime('%Y-%m-%d')
        today = requets.GET.get('today')
        if today:
            try:
                datetime.strptime(today, '%Y-%m-%d')
            except:
                today = default_day
        else:
            today = default_day

        url_ids = requets.GET.get('url_ids')

        if url_ids:
            try:
                url_ids = url_ids.split(',')
                url_ids = [int(u) for u in url_ids]
            except:
                url_ids = None

        paramets = {
            'id':event_id,
            'province': province,
            'today':today
        }

        result = fanwen.get_event_detail(paramets, url_ids=url_ids)
        return JsonResponse({'is_success': True, 'data': result})

    test_id = requets.GET.get('test_id')
    result = fanwen.get_event_detail()
    return render(requets, 'event_detail.html', {'url_list':result, 'id':test_id})


def weather_api(request):
    city = request.GET.get('city', '佛山')
    w = tstools.QueryWeather(city, 1)
    res = w.get()
    content = {
        'city': res['city'],
        'update_time': res['update_time'],
        'date': res['data'][0]['date'],
        'week': res['data'][0]['week'],
        'wea': res['data'][0]['wea'],
        'humidity': res['data'][0]['humidity'],
        'air_level': res['data'][0]['air_level'],
        'tem': res['data'][0]['tem'],
        'tem1': res['data'][0]['tem1'],
        'tem2': res['data'][0]['tem2'],
    }
    return allow_all(JsonResponse({'is_success': True, 'result':content}))

# 高明大屏假数据
def map_news(request):
    today = datetime.today()
    enddate = today.strftime('%Y-%m-%d')
    startdate = (today - timedelta(days=7)).strftime('%Y-%m-%d')

    local_news = News(host=ELASTIC_HOST, index_name=FW_NEW_INDEX, type_name=FW_NEW_TYPE)

    s = local_news.search()
    s = s.source(fields={
        'includes': ["keywords", "news_title", "news_intro", "page","news_auth", "update_time"]
    })
    s = s.filter("term", category_id="126967")
    s = s.filter("match", news_desc="佛山市高明区")


    s = s.query('range', update_time={
        "gte": "{} 00:00:00.000".format(startdate),
        "lte": "{} 23:59:59.999".format(enddate)
    })
    s = s.sort('-update_time')
    response = s[0:10].execute()

    rows = []
    for item in response.hits.hits:
        if "山火" in item['_source']['keywords']:
            item['_source']['position'] = [112.766073, 22.965516]
        elif "火势" in item['_source']['keywords']:
            item['_source']['position'] = [112.770671,22.94601]
        elif "火情" in item['_source']['keywords']:
            item['_source']['position'] = [112.762613,22.981191]
        else:
            places = [[112.898909,22.906059],[112.877744,22.901244],
                      [112.87915,22.892955],[112.884601,22.900536],[112.878753,22.907822]]
            position = sample(places,1)
            item['_source']['position'] = position[0]

        rows.append(item['_source'])

    return allow_all(JsonResponse({
        'is_error': True,
        'msg': '成功',
        'result':{
            'total': response.hits.total,
            'took': response.took,
            'rows': rows
        }
    }))

def tour_fake_api(request):
    year = request.GET.get('year', '2019')
    try:
        year = int(year)
    except:
        year = 2019
    content, pie = tstools.tour_data(year)
    return allow_all(JsonResponse({
        'is_error': True,
        'msg': '成功',
        'data': content,
        'home_data': pie,
    }))

# app热词
def app_hotword(request):
    size = request.GET.get('size', 10)
    enddate = request.GET.get('endDate')
    startdate = request.GET.get('startDate')
    today = datetime.today()
    try:
        enddate = datetime.strptime(enddate,'%Y-%m-%d %H:%M:%S')
        startdate = datetime.strptime(startdate, '%Y-%m-%d %H:%M:%S')
        if startdate > enddate:
            return allow_all(JsonResponse({'is_error':True, 'Msg':"开始时间大于结束时间"}))
    except Exception as e:
        return allow_all(JsonResponse({'is_error': True, 'Msg': "时间格式不对,2020-02-02 00:00:00"}))

    hotword = MessageNews(
        host=ELASTIC_HOST,
        index_name=HOTWORD_INDEX,
        type_name=HOTWORD_TYPE
    )
    res = hotword.hotword_static(
        begin_date=startdate.strftime('%Y-%m-%d %H:%M:%S'),
        end_date=enddate.strftime('%Y-%m-%d %H:%M:%S'),
        size=size
    )
    return allow_all(JsonResponse({'is_error':False, 'Msg': '成功', 'data':res}))


# 分类测试
@csrf_exempt
def news_class_predict(request):
    if request.method == 'POST':
        content = request.POST.get('content')
        is_html = request.POST.get('is_html' '1')
        if is_html == '2':
            is_html = True
        else:
            is_html = False
        return render(
            request,
            'predict_class.html',
            # {"result":check_one(content, is_html), "content":content}
        )
    else:
        ids = request.GET.get('ids')
        if ids:
            print(ids)
            try:
                ids = ids.split(',')
                #return JsonResponse({'is_error': False, 'Msg': '成功', 'data': check_fsp_ids(ids)})
                return JsonResponse({'is_error': False, 'Msg': '成功'})
            except Exception as e:
                print(e)
                pass

        return render(request, 'predict_class.html')


# 文章分析
@csrf_exempt
def news_analyse(request):
    if request.method == 'POST':
        content = request.POST.get('content')
        title = request.POST.get('title')
        res = {
           "content": content,
            "title": title,
            "result": find_keyword(title=title,content=content)
        }
        return render(request, 'news_tag.html', res)
    else:
        return render(request, 'news_tag.html')


# 评论观点抽取
@csrf_exempt
def comment_analyse(request):
    if request.method == 'POST':
        text = request.POST.get('text')
        type_name = request.POST.get('type_name')
        res = {
            "text": text,
            "type_name": type_name,
            "result": comment_tag(text, type_name)
        }
        print(res)
        return render(request, 'comment_tag.html', res)
    else:
        return render(request, 'comment_tag.html')

# 获取图片
@csrf_exempt
def cv_api(request):
    content = None
    if request.method == 'POST':
        img_data = request.FILES.get('file_data')
        method = request.POST.get('method', 'class')
        if img_data:
            cropped_photo = crop_image(img_data)
            img = AiImgModel(
                project='class',
                photo=cropped_photo
            )
            img.save()
            responses = []
            if method == 'class':
                res = class_image(img)
                for key, val in res[0][0].items():
                    tmp_name = trans_api(key)
                    responses.append(tmp_name[0])
                img.predict = responses[0]

            if method == 'garbage':
                res = class_image(img)
                for key, val in res[0][0].items():
                    tmp_name = trans_api(key)
                    tmp_res = rubbish(tmp_name[0])
                    if not tmp_res['is_error']:
                        responses.append([tmp_res['data'], tmp_name[0], key])
                img.predict = responses[0][1]

            img.save()
            content = {'is_error': False, 'result': responses}
        else:
            content = {'is_error': True, 'msg': "缺少文件"}

    return render(request, 'class_image_demo.html', content)

def crop_image(file):
    # 随机生成新的图片名，自定义路径。
    ext = file.name.split('.')[-1]
    file_name = '{}.{}'.format(uuid.uuid4().hex[:10], ext)
    cropped_avatar = os.path.join("ai_img", file_name)
    # 相对根目录路径
    file_path = os.path.join('media', "ai_img", file_name)
    img = Image.open(file)
    crop_im = img.resize((224, 224), Image.ANTIALIAS)
    crop_im.save(file_path)
    return cropped_avatar
